import java.util.ArrayList;
import java.util.List;

/**
 * @author 许喜朝
 */
public class ObserverTest {
    public static void main(String[] args) {
        Subject subject = new Subject();
        subject.addObserver(new Task1());
        subject.addObserver(new Task2());

        subject.notifyObserver("yyyy");
    }
}
//主题对象
class Subject{
    //容器
    List<ObServer> container = new ArrayList<>();
    //add
    public void addObserver(ObServer obServer){
        container.add(obServer);
    }
    //remove
    public void removeObserver(ObServer obServer){
        container.remove(obServer);
    }
    public void notifyObserver(Object object){
        for (ObServer obServer : container) {
            obServer.update(object);
        }
    }
}
//观察者
interface ObServer{
    void update(Object object);
}
//任务1
class Task1 implements ObServer{
    @Override
    public void update(Object object) {
        System.out.println("task1"+object);
    }
}
//任务2
class Task2 implements ObServer{
    @Override
    public void update(Object object) {
        System.out.println("task2"+object);
    }
}